package com.demo.business.banner;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 
 * @create: 2022-02-25 23:30:00
 **/
@Service("banner")
@Slf4j
public class BannerService{

    @Resource
    BannerDao bannerDao;

    /**
     * 添加,获取lBannerId数据
     *
     * @param cdoRequest - lBannerId:bannerid
     *                   - sImg:banner图片
     *                   - sInfo:banner备注
     *                   - nType:banner类型：0：普通banner 1、打开url banner 2、弹窗banner
     *                   - sTargetUrl:打开url
     *                   - nLocation:banner位置
     *                   - nSort:
     *                   - nStatus:0未上架，1 上架，99、下架
     *                   - dCreateTime:创建时间
     *                   - dUpdateTime:更新时间
     * @return
     * @author 
     * @create: 2022-02-25 23:30:00
     */
    @OpenApi
    public Return addBanner(CDO cdoRequest) {
        bannerDao.addBanner(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("lBannerId")) {
            cdoResult.set("lBannerId", cdoRequest.get("lBannerId"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lBannerId，更新数据
     *
     * @param cdoRequest - lBannerId:bannerid
     *                   - sImg:banner图片
     *                   - sInfo:banner备注
     *                   - nType:banner类型：0：普通banner 1、打开url banner 2、弹窗banner
     *                   - sTargetUrl:打开url
     *                   - nLocation:banner位置
     *                   - nSort:
     *                   - nStatus:0未上架，1 上架，99、下架
     *                   - dCreateTime:创建时间
     *                   - dUpdateTime:更新时间
     * @return
     * @author 
     * @create: 2022-02-25 23:30:00
     */
    @OpenApi
    public Return updateBannerById(CDO cdoRequest) {
        int affectedRows = bannerDao.updateBannerById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lBannerId，删除数据
     *
     * @param cdoRequest - lBannerId:bannerid
     * @return
     * @author 
     * @create: 2022-02-25 23:30:00
     */
    //@OpenApi
    public Return deleteBannerById(CDO cdoRequest){
        int affectedRows = bannerDao.deleteBannerById(cdoRequest.getLong("lBannerId"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除数据
     *
     * @param cdoRequest
     * @return
     * @author 
     * @create: 2022-02-25 23:30:00
     */
    //@OpenApi
    public Return deleteBanner(CDO cdoRequest) {
        int affectedRows = bannerDao.deleteBanner(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取数据
     *
     * @param cdoRequest - lBannerId:bannerid
     * @return
     * @author 
     * @create: 2022-02-25 23:30:00
     */
    @OpenApi
    public Return getBannerById(CDO cdoRequest) {
        CDO cdoMapperResult = bannerDao.getBannerById(cdoRequest.getLong("lBannerId"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取数据
     *
     * @param cdoRequest - lBannerId:bannerid
     * @return
     * @author 
     * @create: 2022-02-25 23:30:00
     */
    @OpenApi
    public Return getBanner(CDO cdoRequest) {
        CDO cdoMapperResult = bannerDao.getBanner(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取数据列表
     *
     * @param cdoRequest
     * @return
     * @author 
     * @create: 2022-02-25 23:30:00
     */
    @OpenApi
    public Return getBannerList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = bannerDao.getBannerList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = bannerDao.getBannerListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取数据列表
     *
     * @param cdoRequest - lBannerId:bannerid
     * @return
     * @author 
     * @create: 2022-02-25 23:30:00
     */
    @OpenApi
    public Return getBannerListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = bannerDao.getBannerListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = bannerDao.getBannerListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
